import { ref, reactive, inject } from "vue";
import { useProjectStore } from '../../../store/useProjectStore'

interface Tree {
    id: string;
    label: string;
    value: string;
}

export const useProjectChange = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;

    const changeData = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                projectName: ''
            },
            projectList: ref<Tree[]>([]),
            changeTypes: [
                {
                    value: 1,
                    label: '需求变更',
                },
                {
                    value: 2,
                    label: '进度变更',
                },
                {
                    value: 3,
                    label: '资源变更',
                }
            ],
            infoChangeTypes:[
                {
                    value: 1,
                    label: '新增',
                },
                {
                    value: 2,
                    label: '修正',
                },
                {
                    value: 3,
                    label: '删除',
                }
            ]
        }
    );

    const projectStore = useProjectStore();

    //查询
    const getProjectChanges = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.GetProjectChanges,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { projectName: changeData.queryCondition.projectName },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                changeData.tableData = response.data.data;
                changeData.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //获取创建人所有正在进行的项目
    const GetAllProjectByCreateUser = async (status) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetAllProjectByCreateUser,
                {
                    status: status
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                const transformMenuData: any = (ProjectList: any[]) => {
                    return ProjectList.map((p) => {
                        return {
                            id: p.id,
                            label: p.projectName,
                            value: p.projectName,
                        };
                    });
                };
                changeData.projectList = transformMenuData(response.data.data);
            }
        } catch (error) {
            console.error(error);
        }
    }


    return {
        changeData,
        projectStore,
        getProjectChanges,
        GetAllProjectByCreateUser
    };
};